package com.ggs.rg.ybjb.adminmodule.util;

import com.alibaba.fastjson.JSONObject;
import com.ggs.rg.ybjb.adminmodule.mapper.TOrderMapper;
import com.ggs.rg.ybjb.pojomodule.pojo.TOrder;

import java.awt.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName:OrderUtil
 * Package:com.ggs.rg.ybjb.adminmodule.util
 * Description:
 *
 * @Date:2021/11/2 20:59
 * @Author:cxw
 */
public class OrderUtil {

    /**
     * 将获取的到的订单信息转换为json格式
     * @param tOrder
     * @return
     */
    public static JSONObject orderForJson(TOrder tOrder,Integer status) {
        //遍历,将获取到的订单信息与地址信息放入json中
        JSONObject result = new JSONObject();
        if (status == 1){
            String oid = tOrder.getOid();
//            Integer aid = tOrder.getAid();
            result.put("oid", oid);
            result.put("phone",tOrder.getPhone());
            result.put("status",tOrder.getStatus());
            result.put("address",tOrder.getAddress());
            result.put("consignee_name",tOrder.getConsigneeName());
            result.put("consignee_phone",tOrder.getConsigneePhone());
            result.put("price",tOrder.getPrice());
//            result.put("createTime",tOrder.getCreateTime());
//            这个list用来存放订单中的商品信息(订单可能有多个商品所以用list)
//            List<Map> goodsList = tOrderService.selectAllOrders(oid);
//            result.put("goodsList",goodsList);
        }else {
            String oid = tOrder.getOid();
//            Integer aid = tOrder.getAid();
            result.put("oid", oid);
            result.put("phone",tOrder.getPhone());
            result.put("status",tOrder.getStatus());
            result.put("price",tOrder.getPrice());
            result.put("createTime",tOrder.getCreateTime());
        }
        return  result;
    }

    /**
     * 获取已付款更详细的订单信息
     * @param tOrder
     * @param tOrderMapper
     * @return
     */
    public static JSONObject orderForJson(TOrder tOrder, TOrderMapper tOrderMapper) {
        //遍历,将获取到的订单信息与地址信息放入json中
        JSONObject result = new JSONObject();
        String oid = tOrder.getOid();
//            Integer aid = tOrder.getAid();
        result.put("oid", oid);
        result.put("phone",tOrder.getPhone());
        result.put("status",tOrder.getStatus());
        result.put("address",tOrder.getAddress());
        result.put("consigneeName",tOrder.getConsigneeName());
        result.put("consigneePhone",tOrder.getConsigneePhone());
        result.put("price",tOrder.getPrice());
        result.put("name",tOrder.getName());
        result.put("createTime",tOrder.getCreateTime());
        result.put("updateTime",tOrder.getUpdateTime());
//            result.put("createTime",tOrder.getCreateTime());
//            这个list用来存放订单中的商品信息(订单可能有多个商品所以用list)
        List<HashMap<String,Object>> goodsList = tOrderMapper.selectOrdersInfo(oid);
        for (HashMap map:goodsList){
            if (map.get("image")!=null){
                String image = (String) map.get("image");
                String imageAddr = ImageUtil.getImageAddr(image, "9001");
                map.put("image",imageAddr);
            }
        }
        System.out.println("工具类输出"+ goodsList);
        result.put("goodsList",goodsList);
        return  result;
    }


    /**
     * 获取未付款更详细的订单信息
     * @param tOrder
     * @param tOrderMapper
     * @return
     */
    public static JSONObject unpaidOrderForJson(TOrder tOrder, TOrderMapper tOrderMapper) {
        //遍历,将获取到的订单信息与地址信息放入json中
        JSONObject result = new JSONObject();
        String oid = tOrder.getOid();
//            Integer aid = tOrder.getAid();
        result.put("oid", oid);
        result.put("phone",tOrder.getPhone());
        result.put("status",tOrder.getStatus());
        result.put("price",tOrder.getPrice());
        result.put("name",tOrder.getName());
        result.put("createTime",tOrder.getCreateTime());
        result.put("updateTime",tOrder.getUpdateTime());
        List<HashMap<String,Object>> goodsList = tOrderMapper.selectOrdersInfo(oid);
        for (HashMap map:goodsList){
            if (map.get("image")!=null){
                String image = (String) map.get("image");
                String imageAddr = ImageUtil.getImageAddr(image, "9001");
                map.put("image",imageAddr);
            }
        }
        result.put("goodsList",goodsList);
        return  result;
    }
}
